package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.JingfeiDeptMonth;
import com.ruoyi.system.domain.JingfeiJingfeipiciyue;
import com.ruoyi.system.domain.JingfeiPiciEx;
import com.ruoyi.system.mapper.JingfeiDeptMonthMapper;
import com.ruoyi.system.mapper.JingfeiJingfeipiciyueMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JingfeiJingfeipiciMapper;
import com.ruoyi.system.domain.JingfeiJingfeipici;
import com.ruoyi.system.service.IJingfeiJingfeipiciService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * jingfeipiciService业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Service
public class JingfeiJingfeipiciServiceImpl implements IJingfeiJingfeipiciService 
{
    @Autowired
    private JingfeiJingfeipiciMapper jingfeiJingfeipiciMapper;

    @Autowired
    private JingfeiDeptMonthMapper jingfeiDeptMonthMapper;

    @Autowired
    private JingfeiJingfeipiciyueMapper jingfeiJingfeipiciyueMapper;

    /**
     * 查询jingfeipici
     * 
     * @param jingfeipiciid jingfeipici主键
     * @return jingfeipici
     */
    @Override
    public JingfeiJingfeipici selectJingfeiJingfeipiciByJingfeipiciid(String jingfeipiciid)
    {
        return jingfeiJingfeipiciMapper.selectJingfeiJingfeipiciByJingfeipiciid(jingfeipiciid);
    }

    /**
     * 查询jingfeipici列表
     * 
     * @param jingfeiJingfeipici jingfeipici
     * @return jingfeipici
     */
    @Override
    public List<JingfeiJingfeipici> selectJingfeiJingfeipiciList(JingfeiJingfeipici jingfeiJingfeipici)
    {
        return jingfeiJingfeipiciMapper.selectJingfeiJingfeipiciList(jingfeiJingfeipici);
    }

    /**
     * 新增jingfeipici
     * 
     * @param jingfeiJingfeipici jingfeipici
     * @return 结果
     */
    @Override
    public int insertJingfeiJingfeipici(JingfeiJingfeipici jingfeiJingfeipici)
    {
        return jingfeiJingfeipiciMapper.insertJingfeiJingfeipici(jingfeiJingfeipici);
    }

    /**
     * 修改jingfeipici
     * 
     * @param jingfeiJingfeipici jingfeipici
     * @return 结果
     */
    @Override
    public int updateJingfeiJingfeipici(JingfeiJingfeipici jingfeiJingfeipici)
    {
        return jingfeiJingfeipiciMapper.updateJingfeiJingfeipici(jingfeiJingfeipici);
    }

    /**
     * 批量删除jingfeipici
     * 
     * @param jingfeipiciids 需要删除的jingfeipici主键
     * @return 结果
     */
    @Override
    public int deleteJingfeiJingfeipiciByJingfeipiciids(String jingfeipiciids)
    {
        return jingfeiJingfeipiciMapper.deleteJingfeiJingfeipiciByJingfeipiciids(Convert.toStrArray(jingfeipiciids));
    }

    /**
     * 删除jingfeipici信息
     * 
     * @param jingfeipiciid jingfeipici主键
     * @return 结果
     */
    @Override
    public int deleteJingfeiJingfeipiciByJingfeipiciid(String jingfeipiciid)
    {
        return jingfeiJingfeipiciMapper.deleteJingfeiJingfeipiciByJingfeipiciid(jingfeipiciid);
    }

    @Override
    public List<Map<String,Object>>customlist(String ppDeptId, String ppPiciname, String ppZhuangtai){
        ppPiciname = "%"+ppPiciname+"%";
        List<Map<String,Object>> mmList = jingfeiJingfeipiciMapper.customlist(ppDeptId,ppPiciname,ppZhuangtai);

        for(int i=0;i<mmList.size();i++){
            String mmPiciId = mmList.get(i).get("jingfeipiciid").toString();
            JingfeiJingfeipiciyue mmyue = new JingfeiJingfeipiciyue();
            mmyue.setJingfeipiciid(mmPiciId);
            List<JingfeiJingfeipiciyue>mmYueList = jingfeiJingfeipiciyueMapper.selectJingfeiJingfeipiciyueList(mmyue);
            mmList.get(i).put("monthlist",mmYueList);
        }

        return mmList;
    }

    @Override
    public List<Map<String,Object>>findWFFByDeptId(String ppDeptId){
        List<Map<String,Object>> mmList = jingfeiJingfeipiciMapper.findWFFByDeptId(ppDeptId);

        for(int i=0;i<mmList.size();i++){
            String mmPiciId = mmList.get(i).get("jingfeipiciid").toString();
            JingfeiJingfeipiciyue mmyue = new JingfeiJingfeipiciyue();
            mmyue.setJingfeipiciid(mmPiciId);
            List<JingfeiJingfeipiciyue>mmYueList = jingfeiJingfeipiciyueMapper.selectJingfeiJingfeipiciyueList(mmyue);
            mmList.get(i).put("monthlist",mmYueList);
        }
        return mmList;
    }

    @Override
    @Transactional
    public boolean customadd(JingfeiPiciEx JingfeipiciEx){
        boolean ok = true;

        //添加经费批次
        JingfeiJingfeipici jingfeiJingfeipici = new JingfeiJingfeipici();
        String jingfeipiciid = UUID.randomUUID().toString();
        jingfeiJingfeipici.setJingfeipiciid(jingfeipiciid);
        jingfeiJingfeipici.setDeptid(JingfeipiciEx.getEx_deptid());
        jingfeiJingfeipici.setPicimingcheng(JingfeipiciEx.getEx_picimingcheng());
        jingfeiJingfeipici.setShifarenshu(0l);
        jingfeiJingfeipici.setShifajine(0l);
        jingfeiJingfeipici.setBufajine(0l);
        jingfeiJingfeipici.setKaishinian(JingfeipiciEx.getEx_kaishinian());
        jingfeiJingfeipici.setKaishiyue(JingfeipiciEx.getEx_kaishiyue());
        jingfeiJingfeipici.setJieshunian(JingfeipiciEx.getEx_jieshunian());
        jingfeiJingfeipici.setJieshuyue(JingfeipiciEx.getEx_jieshuyue());
        jingfeiJingfeipici.setJiezhishijian(JingfeipiciEx.getEx_jiezhishijian());
        jingfeiJingfeipici.setIsfafang(0l);
        jingfeiJingfeipici.setIsjisuan(0l);
        if (JingfeipiciEx.getEx_deptid().equals(RuoYiConfig.getZjzxdeptid())){
            jingfeiJingfeipici.setBeizhu1(jingfeiJingfeipici.getJingfeipiciid());
        }else{
            jingfeiJingfeipici.setBeizhu1(JingfeipiciEx.getZjzxpiciId());
        }

        if (insertJingfeiJingfeipici(jingfeiJingfeipici)!=1){
            ok = false;
            throw new RuntimeException("新增经费批次失败");
        }

        List<JingfeiDeptMonth> jingfeiDeptMonthList = JingfeipiciEx.getJingfeiDeptMonthList();
        if (JingfeipiciEx.getEx_deptid().equals(RuoYiConfig.getZjzxdeptid())){
            //是专家中心
            for (int i=0;i<jingfeiDeptMonthList.size();i++){
                //添加经费批次月
                JingfeiJingfeipiciyue jingfeiJingfeipiciyue = new JingfeiJingfeipiciyue();
                jingfeiJingfeipiciyue.setPiciyueid(UUID.randomUUID().toString());
                jingfeiJingfeipiciyue.setJingfeipiciid(jingfeipiciid);
                jingfeiJingfeipiciyue.setZjzxpiciid(jingfeipiciid);
                jingfeiJingfeipiciyue.setDeptid(jingfeiJingfeipici.getDeptid());
                jingfeiJingfeipiciyue.setNianyue(jingfeiDeptMonthList.get(i).getShoukuannianyue());
                if (jingfeiJingfeipiciyueMapper.insertJingfeiJingfeipiciyue(jingfeiJingfeipiciyue)!=1){
                    ok = false;
                    throw new RuntimeException("新增经费批次月失败");
                }
            }
        }else{
            //不是专家中心
            for (int i=0;i<jingfeiDeptMonthList.size();i++){
                //更新经费单位月使用情况
                JingfeiDeptMonth jingfeiDeptMonth = new JingfeiDeptMonth();
                jingfeiDeptMonth.setId(jingfeiDeptMonthList.get(i).getId());
                jingfeiDeptMonth.setIsuse(1l);
                if (jingfeiDeptMonthMapper.updateJingfeiDeptMonth(jingfeiDeptMonth)!=1) {
                    ok = false;
                    throw new RuntimeException("更新经费单位月失败");
                }

                JingfeiDeptMonth mmTemp = jingfeiDeptMonthMapper.selectJingfeiDeptMonthById(jingfeiDeptMonthList.get(i).getId());

                //添加自己的经费批次月
                JingfeiJingfeipiciyue jingfeiJingfeipiciyue = new JingfeiJingfeipiciyue();
                jingfeiJingfeipiciyue.setPiciyueid(UUID.randomUUID().toString());
                jingfeiJingfeipiciyue.setJingfeipiciid(jingfeipiciid);
                jingfeiJingfeipiciyue.setZjzxpiciid(mmTemp.getZjzxpiciid());
                jingfeiJingfeipiciyue.setDeptid(jingfeiJingfeipici.getDeptid());
                jingfeiJingfeipiciyue.setNianyue(jingfeiDeptMonthList.get(i).getShoukuannianyue());
                jingfeiJingfeipiciyue.setBeizhu1(mmTemp.getJingfeipiciid());
                if (jingfeiJingfeipiciyueMapper.insertJingfeiJingfeipiciyue(jingfeiJingfeipiciyue)!=1){
                    ok = false;
                    throw new RuntimeException("新增经费批次月失败");
                }
            }
        }

        return ok;
    }

    @Override
    public List<JingfeiJingfeipici>findPCByDeptIdAndPiciName(String ppDeptId,String ppPiciName){
       if(ppPiciName!=null&&!ppPiciName.equals("")){
           ppPiciName ="%"+ppPiciName+"%";
       }
        return jingfeiJingfeipiciMapper.findPCByDeptIdAndPiciName(ppDeptId,ppPiciName);
    }

    @Override
    @Transactional
    public boolean deleteJingfeipici(String ppPiciId) {
        boolean ok = true;

        //添加NewJingfeiPici
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if (this.deleteJingfeiJingfeipiciByJingfeipiciid(ppPiciId)!=1){
            ok = false;
            throw new RuntimeException("删除批次失败");
        }

        JingfeiJingfeipiciyue mmJingfeiJingfeipiciyue = new JingfeiJingfeipiciyue();
        mmJingfeiJingfeipiciyue.setJingfeipiciid(ppPiciId);
        List<JingfeiJingfeipiciyue> mmList = jingfeiJingfeipiciyueMapper.selectJingfeiJingfeipiciyueList(mmJingfeiJingfeipiciyue);

        for (int i=0;i<mmList.size();i++){
            String mmId = mmList.get(i).getPiciyueid();
            String mmDeptMonthId = mmList.get(i).getBeizhu1();

            JingfeiDeptMonth mmDeptMonth = jingfeiDeptMonthMapper.selectJingfeiDeptMonthById(mmDeptMonthId);
            if(mmDeptMonth!=null){
                mmDeptMonth.setIsuse((long) 0);
                if (jingfeiDeptMonthMapper.updateJingfeiDeptMonth(mmDeptMonth) != 1) {
                    ok = false;
                    throw new RuntimeException("修改单位月失败");
                }
            }

            if (jingfeiJingfeipiciyueMapper.deleteJingfeiJingfeipiciyueByPiciyueid(mmId)!=1){
                ok = false;
                throw new RuntimeException("删除批次月失败");
            }
        }

        return ok;
    }
}
